﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreRay
{
    /// <summary>
    /// Sequential spatial structure only used for reference. Don't use it for production because it is really slow.
    /// </summary>
    public class ReferenceSpatial : SpatialStructure
    {
        private List<GeometryObject> geometryObjects;

        public ReferenceSpatial(bool useWorldMatrix) : base(useWorldMatrix)
        {

        }

        public override void Construct(List<GeometryObject> geometryObjects)
        {
            this.geometryObjects = geometryObjects;
        }

        public override Intersection Intersect(Ray ray)
        {
            Intersection currentIntersection = null;

            for (int i = 0; i < this.geometryObjects.Count; i++)
            {
                Intersection intersection = this.geometryObjects[i].Intersect(ray);

                if (intersection != null && (currentIntersection == null || currentIntersection.RayT > intersection.RayT))
                {
                    currentIntersection = intersection;
                }
            }

            return currentIntersection;
        }

        public override bool IntersectPosition(Ray ray)
        {
            for (int i = 0; i < this.geometryObjects.Count; i++)
            {
                if (this.geometryObjects[i].IntersectPosition(ray))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
